package com.ruoyi.service.impl;

import com.github.pagehelper.PageInfo;
import com.hundun.vision.api.common.PlainResult;
import com.hundun.vision.api.dto.ExchangeDTO;
import com.hundun.vision.api.dto.WalletDTO;
import com.hundun.vision.api.query.WalletQuery;
import com.hundun.vision.api.request.WalletRequest;
import com.hundun.vision.api.service.RPCExchangeService;
import com.hundun.vision.api.service.RPCWalletService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ResultUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.service.WalletService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author ：RuoChen
 * @date ：16:17 2021/6/22
 * @description：交易所钱包服务实现类
 */
@Slf4j
@Component
public class WalletServiceImpl implements WalletService {

    @DubboReference(timeout = 60000, retries = 0)
    private RPCWalletService walletService;

    @DubboReference(timeout = 60000, retries = 0)
    private RPCExchangeService exchangeService;

    @Override
    public Boolean save(WalletRequest request) {
        log.info("save wallet,request:{}", request);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        request.setUserId(user.getUserId());
        PlainResult<Boolean> result = walletService.save(request);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return false;
    }

    @Override
    public Integer removeByIds(List<Long> walletIds) {
        log.info("remove wallet by ids:{}", walletIds);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        PlainResult<Integer> result = walletService.removeByIds(user.getUserId(), walletIds);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return 0;
    }

    @Override
    public Boolean update(WalletRequest request) {
        log.info("save wallet,request:{}", request);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        request.setUserId(user.getUserId());
        PlainResult<Boolean> result = walletService.update(request);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return false;
    }

    @Override
    public WalletDTO getById(Long walletId) {
        log.info("get wallet by id,walletId:{}", walletId);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        PlainResult<WalletDTO> result = walletService.getById(user.getUserId(), walletId);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return null;
    }

    @Override
    public PageInfo<WalletDTO> search(WalletQuery query) {
        log.info("search wallet,query:{}", query);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        query.setUserId(user.getUserId());
        PlainResult<PageInfo<WalletDTO>> result = walletService.search(query);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return null;
    }

    @Override
    public List<ExchangeDTO> listExchanges() {
        log.info("list exchange");
        PlainResult<List<ExchangeDTO>> result = exchangeService.list();
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return result.getData();
    }
}
